Java Class File এর গঠন গাইড ও নোট

Java Technologies - জাভা ভার্চুয়াল মেশিন (Java Virtual Machine) - JVM এর Class File Structure
387

Java Class ফাইল হল bytecode ধারণকারী একটি ফাইল, যা Java Source Code (.java ফাইল) কম্পাইল করার মাধ্যমে তৈরি হয়। এটি .class এক্সটেনশন নিয়ে থাকে এবং JVM (Java Virtual Machine) দ্বারা এক্সিকিউট করা হয়। Java Class File হলো Java bytecode যা প্ল্যাটফর্ম-স্বাধীন। এটি কোনো নির্দিষ্ট অপারেটিং সিস্টেমের জন্য নয়, বরং যেকোনো প্ল্যাটফর্মে JVM এর মাধ্যমে এক্সিকিউট করা যায়।

Java Class File এর গঠন:

একটি Java Class ফাইলের গঠন বেশ কয়েকটি অংশে বিভক্ত, যা নিম্নরূপ:

  1. Magic Number:
    • Magic Number হল প্রথম 4 বাইট যা .class ফাইলের শুরুতে থাকে এবং এটি Java class ফাইলটির বৈধতা যাচাই করতে ব্যবহৃত হয়।
    • এটি একটি স্ট্যাটিক প্যাটার্ন: 0xCAFEBABE
  2. Minor Version and Major Version:
    • পরবর্তী দুটি বাইট Minor Version এবং Major Version এর জন্য সংরক্ষিত থাকে, যা Java class ফাইলের সংস্করণ (version) নির্দেশ করে।
    • উদাহরণস্বরূপ, JDK 8 এর জন্য major version 52 এবং minor version 0 থাকে।
    • Major Version:
      • JDK 1.0: 45
      • JDK 1.1: 45.3
      • JDK 1.2: 46
      • JDK 1.3: 47
      • JDK 1.4: 48
      • JDK 5: 49
      • JDK 6: 50
      • JDK 7: 51
      • JDK 8: 52
      • JDK 9: 53
      • JDK 10: 54
      • JDK 11: 55
  3. Constant Pool:
    • Constant Pool হল একটি টেবিল যা class ফাইলের মধ্যে ব্যবহৃত সমস্ত স্থির মান (constant values) ধারণ করে, যেমন লিটারেল ভ্যালু, ক্লাস, মেথড, ফিল্ড রেফারেন্স ইত্যাদি।
    • এটি class ফাইলের পরবর্তী অংশের জন্য মেমরি এড্রেস হিসাবে কাজ করে।
  4. Access Flags:
    • Access Flags ২ বাইট দৈর্ঘ্য হয়ে থাকে এবং এটি class, মেথড বা ফিল্ডের অ্যাক্সেস কন্ট্রোল এবং বৈশিষ্ট্য নির্ধারণ করে। এটি বুঝতে সাহায্য করে যে:
      • Class public বা final কিনা
      • মেথড static বা abstract কিনা
      • ফিল্ড final কিনা
  5. This Class and Super Class:
    • এই অংশে class এর নিজের রেফারেন্স (this class) এবং superclass এর রেফারেন্স (super class) দেওয়া থাকে। এটি class এর হায়ারার্কি দেখায়।
  6. Interfaces:
    • যদি class কোনো interface ইমপ্লিমেন্ট করে থাকে, তবে তার রেফারেন্স এই অংশে থাকে। একাধিক interfaces থাকলে তাদের রেফারেন্স একটি তালিকা (list) আকারে থাকে।
  7. Fields (ফিল্ড):
    • Fields অংশে class এর সমস্ত ফিল্ডের তথ্য থাকে, যেমন:
      • ফিল্ডের নাম
      • ফিল্ডের টাইপ
      • ফিল্ডের অ্যাক্সেস ফ্ল্যাগ (যেমন public, private, static, final)
  8. Methods (মেথড):
    • Methods অংশে class এর সমস্ত মেথডের তথ্য থাকে। এতে থাকে:
      • মেথডের নাম
      • মেথডের প্যারামিটার টাইপ
      • মেথডের রিটার্ন টাইপ
      • মেথডের অ্যাক্সেস ফ্ল্যাগ (যেমন public, private, static, final)
  9. Attributes:
    • Attributes অংশে class, methods, এবং fields এর সাথে সম্পর্কিত অতিরিক্ত তথ্য থাকে, যেমন:
      • Code attribute: মেথডের bytecode।
      • Exceptions attribute: যেগুলি exception throw হতে পারে।
      • LineNumberTable: বাইটকোডের সাথে সঙ্গতিপূর্ণ সোর্স কোডের লাইনের জন্য টেবিল।
      • LocalVariableTable: লোকাল ভেরিয়েবলগুলির তথ্য।
  10. Bytecode:
    • এটি class ফাইলের অন্যতম গুরুত্বপূর্ণ অংশ, যেখানে ক্লাসের কার্যকর কোড (bytecode) থাকে যা JVM দ্বারা এক্সিকিউট হয়। মেথডের কাজের জন্য এটি নির্দিষ্ট ইনস্ট্রাকশন ধারণ করে।

Java Class File এর গঠন (Structure):

  1. Magic Number (4 bytes): 0xCAFEBABE
  2. Minor Version (2 bytes)
  3. Major Version (2 bytes)
  4. Constant Pool (variable length)
  5. Access Flags (2 bytes)
  6. This Class (2 bytes)
  7. Super Class (2 bytes)
  8. Interfaces (variable length)
  9. Fields (variable length)
  10. Methods (variable length)
  11. Attributes (variable length)
  12. Bytecode (variable length)

Java Class File Example:

একটি সাধারণ Java ক্লাসের কম্পাইল করা .class ফাইলের গঠন উদাহরণ:

Java Source Code:

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

Generated Bytecode (Class File):

  • Magic Number: CAFEBABE
  • Major Version: 52 (Java 8)
  • Constant Pool: Contains string constants like "HelloWorld", "main", etc.
  • Access Flags: public, class
  • Methods: Contains method information like main method.
  • Bytecode: The actual instructions that the JVM will execute (e.g., getstatic, invokevirtual, etc.).

Class File Format Summary:

SectionDescription
Magic Number0xCAFEBABE - Identifies the class file format.
Minor & Major VersionSpecifies the version of the class file format.
Constant PoolContains all constants used in the class (strings, field, method references).
Access FlagsSpecifies the access level of the class (public, private, static, etc.).
This Class & Super ClassReferences to the current class and its superclass.
InterfacesList of interfaces implemented by the class.
FieldsContains the fields (variables) defined in the class.
MethodsContains method details (name, parameters, return type).
AttributesIncludes code and other metadata attributes (e.g., line number, local variables).
BytecodeActual bytecode instructions that are executed by the JVM.

Java Class ফাইল একটি অত্যন্ত গুরুত্বপূর্ণ উপাদান, যেটি Java source code কম্পাইল করার মাধ্যমে তৈরি হয়। এই ফাইলের গঠনটি নির্দিষ্ট ফর্ম্যাটে থাকে, যার মধ্যে magic number, versioning, constant pool, methods, fields, এবং bytecode সহ অনেক অন্যান্য তথ্য থাকে। JVM এই .class ফাইলটি লোড করে এবং bytecode এক্সিকিউট করে Java প্রোগ্রাম চালায়।

Content added By
Promotion

Are you sure to start over?

Loading...